"""add_recommended_apps_category_join_extend

Revision ID: 002_recommended_apps_category
Revises: 001_recommended_list_sorted
Create Date: 2024-07-18 06:38:49.622100

"""
import sqlalchemy as sa
from alembic import op
from sqlalchemy.engine.reflection import Inspector

from models import types

# revision identifiers, used by Alembic.
revision = '002_recommended_apps_category'
down_revision = '001_recommended_list_sorted'
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    conn = op.get_bind()
    inspector = Inspector.from_engine(conn)
    tables = inspector.get_table_names()
    
    if 'recommended_apps_category_join_extend' not in tables:
        op.create_table('recommended_apps_category_join_extend',
        sa.Column('id', types.StringUUID(), server_default=sa.text('uuid_generate_v4()'), nullable=False),
        sa.Column('recommended_id', types.StringUUID(), nullable=False),
        sa.Column('category_id', types.StringUUID(), nullable=False),
        sa.PrimaryKeyConstraint('id', name='recommended_apps_category_id_pkey')
        )
        with op.batch_alter_table('recommended_apps_category_join_extend', schema=None) as batch_op:
            batch_op.create_index('idx_recommended_category_id', ['category_id'], unique=False)
            batch_op.create_index('idx_recommended_id', ['recommended_id'], unique=False)

    if 'recommended_category_extend' not in tables:
        op.create_table('recommended_category_extend',
        sa.Column('id', types.StringUUID(), server_default=sa.text('uuid_generate_v4()'), nullable=False),
        sa.Column('table', sa.String(length=255), nullable=False),
        sa.Column('tag_id', types.StringUUID(), nullable=True),
        sa.PrimaryKeyConstraint('id', name='category_extend_id_pkey')
        )
        with op.batch_alter_table('recommended_category_extend', schema=None) as batch_op:
            batch_op.create_index('idx_extend_table', ['table'], unique=False)
            batch_op.create_index('idx_extend_tag_bind_tag_id', ['tag_id'], unique=False)
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###

    with op.batch_alter_table('recommended_category_extend', schema=None) as batch_op:
        batch_op.drop_index('idx_extend_tag_bind_tag_id')
        batch_op.drop_index('idx_extend_table')

    op.drop_table('recommended_category_extend')
    with op.batch_alter_table('recommended_apps_category_join_extend', schema=None) as batch_op:
        batch_op.drop_index('idx_recommended_id')
        batch_op.drop_index('idx_recommended_category_id')

    op.drop_table('recommended_apps_category_join_extend')
    # ### end Alembic commands ###
